package com.junzhiit.nengji.dao.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import com.bainaeco.framework.base.dao.AbstractBaseDAO;
import com.bainaeco.framework.common.jdbc.StatementParameter;
import com.bainaeco.framework.enums.IsDelete;
import com.junzhiit.nengji.bo.PrinterBO;
import com.junzhiit.nengji.dao.PrinterDAO;
import com.junzhiit.nengji.model.Printer;

@Repository
public class PrinterDAOImpl extends AbstractBaseDAO<Printer> implements PrinterDAO{
	@Override
	public List<PrinterBO> findPageAll(Printer printer,int pageNumber, int maxPageRow){
		
		StringBuffer sql=new StringBuffer();
		sql.append(" select p.*,pt.name printer_template_name from printer p");
		sql.append(" left join printer_template pt on pt.id=p.printer_template_id");
		sql.append(" where p.is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(printer.getShopId())){
			sql.append(" and p.shop_id=?");
			param.setString(printer.getShopId());
		}
		
		sql.append(" order by created_time desc");
		
		return super.queryForPagingList(sql.toString(),PrinterBO.class,param,pageNumber, maxPageRow);
	}
	
	@Override
	public int getTotalCount(Printer printer){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from printer p");
		sql.append(" left join printer_template pt on pt.id=p.printer_template_id");
		sql.append(" where p.is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(printer.getShopId())){
			sql.append(" and p.shop_id=?");
			param.setString(printer.getShopId());
		}
		
		return super.queryForInt(sql.toString(),param);
	}
	
	@Override
	public int getCount(Printer printer){
		StringBuffer sql=new StringBuffer();
		sql.append(" select count(*) from printer");
		sql.append(" where is_delete=?");
		
		StatementParameter param=new StatementParameter();
		param.setInt(IsDelete.NO.toIntValue());
		
		if(StringUtils.isNotBlank(printer.getShopId())){
			sql.append(" and shop_id=?");
			param.setString(printer.getShopId());
		}
		
		if(StringUtils.isNotBlank(printer.getDeviceNumber())){
			sql.append(" and device_number=?");
			param.setString(printer.getDeviceNumber());
		}
		
		if(printer.getType()!=null){
			sql.append(" and type=?");
			param.setInt(printer.getType());
		}
		
		return super.queryForInt(sql.toString(),param);
	}

	@Override
	public List<Printer> getPrinterByShopId(String shopId,Integer type) {
		StringBuffer sql=new StringBuffer();
		StatementParameter param=new StatementParameter();
		sql.append(" select * from printer");
		sql.append(" where shop_id = ? and type = ? and is_delete=?");
		param.setString(shopId);
		param.setInt(type);
		param.setInt(IsDelete.NO.toIntValue());
		return super.queryForList(sql.toString(), Printer.class, param);
	}
}
